// app.ts
const request = require('utils/request.js')

interface IAppOption {
  globalData: {
    userInfo?: WechatMiniprogram.UserInfo,
    [key: string]: any
  }
}
App<IAppOption>({
  globalData: {},
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        console.log(res.code)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        request.postRequest('app/user/wxLogin', { code: res.code })
          .then((response: any) => {
            this.globalData.userInfo = response.user;
            this.globalData.token = response.token;
          })
          .catch((error: any) => {
            console.error('Error:', error);
          });
      },
    })
  },
})